package com.bzframework.pro.service;

import com.bzframework.pro.constant.CommonConstants;
import com.bzframework.pro.model.school.token.AccessToken;
import com.bzframework.pro.model.tanma.TanMaResponse;
import com.bzframework.pro.utils.SignUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;

import java.time.Duration;

/**
 * @Author bingzhong
 * @Date 2025/3/30 14:56
 * @Description
 */
@Slf4j
@Service
public class HttpService {


    /**
     * post请求探马 test
     *
     * @param uri
     * @param bodyStr
     * @param responseClass
     * @param <T>
     * @return
     */
    public <T> Mono<T> postForTanMaTest(String uri, String bodyStr, Class<T> responseClass) {

        long currentTimeMillis = System.currentTimeMillis();
        String sign = SignUtils.genSign(CommonConstants.TAN_MA_APP_ID, currentTimeMillis, bodyStr, CommonConstants.TAN_MA_APP_KEY);
        log.info("sign:{}", sign);
        return WebClient.create(CommonConstants.TAN_MA_TEST_URL_PREFIX)
                        .post()
                        .uri(uriBuilder -> uriBuilder.path(uri).build())
                        .headers(httpHeaders -> {
                            httpHeaders.add("appId", CommonConstants.TAN_MA_APP_ID);
                            httpHeaders.add("timestamp", String.valueOf(currentTimeMillis));
                            httpHeaders.add("sign", sign);
                            httpHeaders.add("appKey", CommonConstants.TAN_MA_APP_KEY);
                            httpHeaders.add("Content-Type", "application/json");
                        })
                        .bodyValue(bodyStr)
                        .retrieve()
                        .bodyToMono(responseClass)
                        .timeout(Duration.ofSeconds(20))
                        .retryWhen(Retry.backoff(3, Duration.ofSeconds(1)));

    }


    /**
     * post请求探马
     *
     * @param uri
     * @param bodyStr
     * @param responseClass
     * @param <T>
     * @return
     */
    public <T> Mono<T> postForTanMaProduce(String uri, String bodyStr, Class<T> responseClass) {

        long currentTimeMillis = System.currentTimeMillis();
        String sign = SignUtils.genSign(CommonConstants.TAN_MA_APP_ID_PRODUCE, currentTimeMillis, bodyStr, CommonConstants.TAN_MA_APP_KEY_PRODUCE);
//        log.info("sign:{}, body:{}", sign, bodyStr);
        return WebClient.create(CommonConstants.TAN_MA_TEST_URL_PREFIX)
                        .post()
                        .uri(uriBuilder -> uriBuilder.path(uri).build())
                        .headers(httpHeaders -> {
                            httpHeaders.add("appId", CommonConstants.TAN_MA_APP_ID_PRODUCE);
                            httpHeaders.add("timestamp", String.valueOf(currentTimeMillis));
                            httpHeaders.add("sign", sign);
                            httpHeaders.add("appKey", CommonConstants.TAN_MA_APP_KEY_PRODUCE);
                            httpHeaders.add("Content-Type", "application/json");
                        })
                        .bodyValue(bodyStr)
                        .retrieve()
                        .bodyToMono(responseClass);
//                        .timeout(Duration.ofSeconds(20))
//                        .retryWhen(Retry.backoff(3, Duration.ofSeconds(1)));

    }

    public <T> Mono<T> postForTanMaProduceOrder(String uri, String bodyStr, Class<T> responseClass) {

        long currentTimeMillis = System.currentTimeMillis();
        String sign = SignUtils.genSign(CommonConstants.TAN_MA_APP_ID_PRODUCE, currentTimeMillis, bodyStr, CommonConstants.TAN_MA_APP_KEY_PRODUCE);
        log.info("sign:{}, body:{}", sign, bodyStr);
        return WebClient.create(CommonConstants.TAN_MA_TEST_ORDER_URL_PREFIX)
                        .post()
                        .uri(uriBuilder -> uriBuilder.path(uri).build())
                        .headers(httpHeaders -> {
                            httpHeaders.add("appId", CommonConstants.TAN_MA_APP_ID_PRODUCE);
                            httpHeaders.add("timestamp", String.valueOf(currentTimeMillis));
                            httpHeaders.add("sign", sign);
                            httpHeaders.add("appKey", CommonConstants.TAN_MA_APP_KEY_PRODUCE);
                            httpHeaders.add("Content-Type", "application/json");
                        })
                        .bodyValue(bodyStr)
                        .retrieve()
                        .bodyToMono(responseClass);
//                        .timeout(Duration.ofSeconds(20))
//                        .retryWhen(Retry.backoff(3, Duration.ofSeconds(1)));

    }

    /**
     * 获取校管家token
     * @return
     */
    public Mono<AccessToken> fetchSchoolAccessToken() {

        return WebClient.create(CommonConstants.SCHOOL_HOUSEKEEPER_PRODUCTION_URL)
                        .get()
                        .uri(uriBuilder -> uriBuilder.path("/token")
                                                     .queryParam("appid", CommonConstants.SCHOOL_APP_ID)
                                                     .queryParam("secret", CommonConstants.SCHOOL_APP_SECRET)
                                                     .build())
                        .retrieve()
                        .bodyToMono(AccessToken.class);

    }




}
